package com.molichuxing.gateway.api.services.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.bff.dealer.entity.response.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.result.ResponseCodeEnum;
import com.molichuxing.framework.result.ResponseResult;
import com.molichuxing.gateway.api.services.DealerApiService;
import com.molichuxing.gateway.bff.dealer.services.DeviceBffService;
import com.molichuxing.gateway.bff.dealer.services.StoreBffService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 经销商相关接口
 *
 * @author zhaopenghui
 * @date 2019年9月18日
 */
@Api(tags = "经销商相关接口")
@RestController
@RequestMapping(value = "/dealer")
public class DealerApiServiceImpl extends RootApiService implements DealerApiService {
    private static final Logger logger = LoggerFactory.getLogger(DealerApiServiceImpl.class);

    @Resource
    StoreBffService storeBffService;
    @Resource
    DeviceBffService deviceBffService;


    @Override
    @ApiOperation(value = "城市获取门店")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "baiduCode", value = "当前定位城市百度城市code码", required = false, dataType = "String"),
            @ApiImplicitParam(name = "longitude", value = "当前定位设备经度", required = false, dataType = "String"),
            @ApiImplicitParam(name = "dimension", value = "当前定位设备纬度", required = false, dataType = "String"),
            @ApiImplicitParam(name = "regionId", value = "当前定位城市id", required = false, dataType = "Integer"),
    })
    @GetMapping(value = "/store/city")
    public ResponseResult<List<StoreInfoVo>> getStoreInfoList(@RequestParam Map<String, Object> map) {

        ResponseResult<List<StoreInfoVo>> result = new ResponseResult<>();
        try {
            String baiduCode = ObjectCastUtil.castString(map.get("baiduCode"));
            Integer regionId = ObjectCastUtil.castInteger(map.get("regionId"));
            if (StringUtils.isBlank(baiduCode) && null == regionId) {
                logger.error("[getStoreInfoList]城市id和百度code不能同时为空");
                return result.error("城市id和百度code不能同时为空");
            }

            return result.success(storeBffService.getStoreInfoList(map));
        } catch (BizException e) {
            logger.error("[getStoreInfoList]城市获取门店异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getStoreInfoList]城市获取门店异常", e);
            result.error("城市获取门店异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "城市获取门店(新版本)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "baiduCode", value = "当前定位城市百度城市code码", required = false, dataType = "String"),
            @ApiImplicitParam(name = "longitude", value = "当前定位设备经度", required = false, dataType = "String"),
            @ApiImplicitParam(name = "dimension", value = "当前定位设备纬度", required = false, dataType = "String"),
            @ApiImplicitParam(name = "regionId", value = "当前定位城市id", required = false, dataType = "Integer"),
    })
    @GetMapping(value = "/store/detail/city")
    public ResponseResult<StoreDetailVo> getStoreDetailList(@RequestParam Map<String, Object> map) {

        ResponseResult<StoreDetailVo> result = new ResponseResult<>();
        try {
            String baiduCode = ObjectCastUtil.castString(map.get("baiduCode"));
            Integer regionId = ObjectCastUtil.castInteger(map.get("regionId"));
            if (StringUtils.isBlank(baiduCode) && null == regionId) {
                logger.error("[getStoreInfoList]城市id和百度code不能同时为空");
                return result.error("城市id和百度code不能同时为空");
            }

            return result.success(storeBffService.getStoreDetailList(map));
        } catch (BizException e) {
            logger.error("[getStoreInfoList]城市获取门店异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getStoreInfoList]城市获取门店异常", e);
            result.error("城市获取门店异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "查询4s门店信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shortName", value = "门店名称", required = true, dataType = "String"),
    })
    @GetMapping(value = "/store/fours/store")
    public ResponseResult<List<StoreNameVo>> getFoursStore(@RequestParam String shortName) {
        ResponseResult<List<StoreNameVo>> result = new ResponseResult<>();
        try {
            return result.success(storeBffService.getFoursStore(shortName));
        } catch (BizException e) {
            logger.error("[getFoursStore]查询4s门店信息异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getFoursStore]查询4s门店信息异常", e);
            result.error("查询4s门店信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "智能终端支付设备信息")
    @GetMapping(value = "/device/unionpay/param")
    public ResponseResult<DeviceUnionpayParamVo> deviceUnionpayParam(
            @RequestHeader(value = "deviceNum", required = true) String deviceNum) {
        ResponseResult<DeviceUnionpayParamVo> result = new ResponseResult<>();
        try {
            return result.success(deviceBffService.getDeviceUnionpayParam(deviceNum));
        } catch (BizException e) {
            logger.error("[deviceUnionpayParam]查询智能终端支付设备信息失败:", e);
            result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
                    e.getMessage());
        } catch (Exception e) {
            logger.error("[deviceUnionpayParam]查询智能终端支付设备信息异常", e);
            result.error("查询智能终端支付设备信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "通过cpu查询智能终端信息")
    @GetMapping(value = "/device/info")
	public ResponseResult<DeviceInfoVo> getDeviceInfo(
			@RequestHeader(value = "deviceNum", required = true) String deviceNum) {
        ResponseResult<DeviceInfoVo> result = new ResponseResult<>();
        try {
			return result.success(deviceBffService.getDeviceInfo(deviceNum));
        } catch (BizException e) {
            logger.error("[getFoursStore]查询4s门店信息异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getFoursStore]查询4s门店信息异常", e);
            result.error("查询4s门店信息异常");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "根据门店id获取门店信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", required = true, dataType = "Integer"),
    })
    @GetMapping(value = "/store/detail")
    public ResponseResult<StoreVo> getStoreById(@RequestParam Integer storeId) {
        ResponseResult<StoreVo> result = new ResponseResult<>();
        try {
            return result.success(storeBffService.getStoreById(storeId));
        } catch (BizException e) {
            logger.error("[getStoreById]根据门店id获取门店信息失败:", e);
            result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
                    e.getMessage());
        } catch (Exception e) {
            logger.error("[getStoreById]根据门店id获取门店信息失败", e);
            result.error("根据门店id获取门店信息失败");
        }
        return result;
    }

    @Override
    @ApiOperation(value = "检验门店、商品状态。 1：正常，2：门店暂停营业，3：门店强制关店，4：商品禁售")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "门店ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "goodsId", value = "商品ID", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/store/goods/check")
    public ResponseResult<Integer> checkGoodsStoreStatus(@RequestParam Integer storeId, @RequestParam Integer goodsId) {
        ResponseResult<Integer> result = new ResponseResult<>();
        try {
            return result.success(storeBffService.checkGoodsStoreStatus(storeId, goodsId));
        } catch (BizException e) {
            logger.error("[getStoreById]根据门店id获取门店信息失败:", e);
            result.error(e.getExceptionCode() == null ? ResponseCodeEnum.UNKNOWN_ERROR.getCode() : e.getExceptionCode(),
                    e.getMessage());
        } catch (Exception e) {
            logger.error("[getStoreById]根据门店id获取门店信息失败", e);
            result.error("根据门店id获取门店信息失败");
        }
        return result;
    }


    @Override
    @ApiOperation(value = "获取监控平台是否开启状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cpu", value = "cpu号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "appVersion", value = "app版本号", required = false, dataType = "String"),
            @ApiImplicitParam(name = "appPatchVersion", value = "app补丁版本", required = false, dataType = "String"),
    })
    @GetMapping(value = "/device/status")
    public ResponseResult<DeviceInfoVo> getDeviceStatus(@RequestParam Map<String, Object> map) {
        ResponseResult<DeviceInfoVo> result = new ResponseResult<>();
        try {
            return result.success(deviceBffService.getDeviceStatus(map));
        } catch (BizException e) {
            logger.error("[getDeviceStatus]查询监控平台是否开启状态异常", e);
            result.error(e.getMessage());
        } catch (Exception e) {
            logger.error("[getDeviceStatus]查询监控平台是否开启状态异常", e);
            result.error("查询监控平台是否开启状态异常");
        }
        return result;
    }


}
